export default {
    data() {
        return {
            isShowUpdate: false,
            updateList: {},
            tableData: [],
            multipleSelection: [],
            isShowDetail: false,
            isShowAdd: false,
            detailList: [],
            formList: {
                productName: '',
                date: '',
                description: '',
                author: '',
                imgPath: '',
                price: '',
                stock: '',
                type: '',
                press: ''
            },
            options: [
                { value: '小说', label: '小说' },
                { value: '艺术', label: '艺术' },
                { value: '历史', label: '历史' },
                { value: '法律', label: '法律' },
                { value: '心理', label: '心理' },
                { value: '儿童文学', label: '儿童文学' },
                { value: '选动漫绘本项5', label: '动漫绘本' },
                { value: '启蒙益智', label: '启蒙益智' },
                { value: '中小教辅', label: '中小教辅' },
                { value: '字典词典', label: '字典词典' },
                { value: '时尚娱乐', label: '时尚娱乐' },
                { value: '养生保健', label: '养生保健' },
                { value: '管理', label: '管理' },
                { value: '经济', label: '经济' }
            ],
            pageSize: 100,
            curpage: 1,
            allcount: 0,
            searchResult: '',
            isShowSearch: false,
            currentPageData: [],
        }
    },
    created() {
        this.getAllBooks()
    },
    methods: {
        handleCurrentChange(val) {
            let pageSize = 6 // 每页显示的数量
            let currentPage = 1 // 当前页码
            let totalData = this.tableData // 全部图书数据
            let totalPages = Math.ceil(totalData.length / pageSize) // 总共的页码数
            // 分页计算
            let start = (currentPage - 1) * pageSize
            let end = start + pageSize
            // 页码选择器
            if (val >= 1 && val <= totalPages) {
                currentPage = val
                start = (currentPage - 1) * pageSize
                end = start + pageSize
                this.currentPageData = totalData.slice(start, end)
            }
        },
        timeTrans(time, type) {
            let date = new Date(new Date(time).getTime() + 8 * 3600 * 1000)
            date = date.toJSON();
            if (type === 1) {
                date = date.substring(0, 10)
            }
            else {
                date = date.substring(0, 19).replace('T', ' ')
            }
            return date
        },
        clearForm() {
            this.updateList = {}
            this.formList = {
                productName: '',
                date: '',
                description: '',
                author: '',
                imgPath: '',
                price: '',
                stock: '',
                type: '',
                press: ''
            }
        },
        submitForm() {
            this.formList.date = this.timeTrans(this.formList.date, 1)
            let book = this.formList
            this.axios.defaults.headers.common['Authorization'] = localStorage['token']
            this.axios.post('api/addBooks',
                { book: book })
                .then(res => {
                    this.$message({
                        type: 'success',
                        duration: 1000,
                        message: '图书添加成功'
                    });
                    this.getAllBooks()
                    this.isShowAdd = false
                    this.clearForm()
                })
                .catch(err => {
                    this.$message({
                        type: 'error',
                        duration: 1000,
                        message: '图书添加失败'
                    });
                })
        },

        addBook() {
            this.isShowAdd = true
        },
        indexMethod(index) {
            return index + 1;
        },
        handleClose(done) {
            done();
        },
        getDetail(e) {
            this.detailList = []
            console.log('getDetaile', e)
            this.detailList.push(e)
            this.isShowDetail = true
        },
        getAllBooks() {
            this.axios.defaults.headers.common['Authorization'] = localStorage['token'];
            this.axios.get('api/getAllBooks')
                .then(res => {
                    this.tableData = res.data.data;
                    console.log(' this.tableData ', this.tableData)
                    this.currentPageData = this.tableData.slice(0, 6)
                })
        },
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleRowSelection(row);
                });
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        submitUpdateForm() {
            console.log('this.updateList', this.updateList)
            let updateBook = this.updateList
            this.axios.defaults.headers.common['Authorization'] = localStorage['token']
            this.axios.post('api/updBooks',
                { updateBook: updateBook })
                .then(res => {
                    this.$message({
                        type: 'success',
                        duration: 1000,
                        message: '图书修改成功'
                    });
                    this.getAllBooks()
                    this.isShowUpdate = false
                })
                .catch(err => {
                    this.$message({
                        type: 'error',
                        duration: 1000,
                        message: '图书修改失败'
                    });
                })
        },
        updateRow(e) {
            this.isShowUpdate = true
            this.updateList = e
            this.updateList.date = this.timeTrans(this.updateList.date, 1)
        },
        // 删除
        deleteRow(e) {
            console.log('deleteRowe:', e)
            this.$confirm('图书删除后不可恢复, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.axios.defaults.headers.common['Authorization'] = localStorage['token']
                this.axios.delete('api/delBooks', { params: { id: e } })
                    .then(res => {
                        this.getAllBooks()
                        this.$message({
                            type: 'success',
                            duration: 1000,
                            message: '删除成功'
                        });
                    })
            }).catch(() => {
            });
        },
    }
}